//
//  TTDiamondDetailZuanshiImageInfoViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/10/28.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDiamondDetailZuanshiImageInfoViewCell.h"
#import <UIImageView+WebCache.h>
#import <SDWebImage.h>
#import "LLWebImageActivityIndicator.h"
#import "YBImageBrowser.h"

#define zuanshiTag 777777

@interface TTDiamondDetailZuanshiImageInfoViewCell ()

@property (nonatomic, strong) NSArray *titlesArray;
@property (nonatomic, strong) NSMutableArray *imageViewsArray;

@end

@implementation TTDiamondDetailZuanshiImageInfoViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        self.titlesArray = @[@"毛胚图", @"高清图", @"特征图", @"比例图"];
        self.imageViewsArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    UIView *view = tap.view;
    NSInteger index = view.tag - zuanshiTag;
    
    NSMutableArray *imgs = [NSMutableArray array];
    for (int i = 0; i < self.imageUrlsArray.count; i++) {
        YBIBImageData *imageData = [YBIBImageData new];
        imageData.imageURL = [NSURL URLWithString:self.imageUrlsArray[i]];
        [imgs addObject:imageData];
    }
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = imgs;
    browser.currentPage = index;
    [browser show];
}

- (void)setImageUrlsArray:(NSArray *)imageUrlsArray
{
    // 防止重用时多次赋值
    if (_imageUrlsArray.count > 0) {
        return;
    }
    _imageUrlsArray = imageUrlsArray;
    if (imageUrlsArray.count != self.titlesArray.count) {
        return;
    }
    
    CGFloat leftSpace = 10;
    CGFloat space = 5;
    CGFloat w = (SCREEN_WIDTH - 2 * leftSpace - 3 * space) / imageUrlsArray.count;
    for (int i = 0; i < imageUrlsArray.count; i++) {
        // 阴影背景
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(leftSpace + i * (w + space), 10, w, w)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.cornerRadius = 5;
        bgView.layer.shadowColor = UIColorHex(6822B7).CGColor;
        bgView.layer.shadowOpacity = 0.2;
        bgView.layer.shadowOffset = CGSizeZero;
        bgView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds cornerRadius:5].CGPath;
        [self.contentView addSubview:bgView];
        // 图片
        NSString *imageUrlString = [imageUrlsArray[i] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        UIImageView *pictureImageView = [[UIImageView alloc] initWithFrame:bgView.frame];
//        pictureImageView.contentMode = UIViewContentModeScaleAspectFit;
        pictureImageView.layer.cornerRadius = 5;
        pictureImageView.layer.masksToBounds = YES;
        pictureImageView.tag = zuanshiTag + i;
        pictureImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:pictureImageView];
        [self.imageViewsArray addObject:pictureImageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [pictureImageView addGestureRecognizer:tap];
        
        // 标题
        UILabel *label = [UILabel labelTextCenterWithTitle:self.titlesArray[i] color:UIColorHex(999999) fontSize:11];
        label.frame = CGRectMake(pictureImageView.left, pictureImageView.bottom + 5, w, 20);
        [self.contentView addSubview:label];
        
        
        LLWebImageActivityIndicator *activity = [[LLWebImageActivityIndicator alloc] init];
        pictureImageView.sd_imageIndicator = activity;
        
        [pictureImageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString] placeholderImage:[UIImage imageNamed:@"zanwutupian"] options:SDWebImageRetryFailed];
        
        
        
    }
    
    
    
}

@end
